package com.shanlitech.bluetooth;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.shanlitech.bluetooth.calibration.CalibrationScanner;
import com.shanlitech.lbs.calibration.ScanAnalysis;
import com.shanlitech.bluetooth.map.Map2D;
import com.shanlitech.bluetooth.map.MapMarker;
import com.shanlitech.lbs.map.BleBeacon;
import com.shanlitech.lbs.map.LbsDatabase;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BeaconCheckFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BeaconCheckFragment extends Fragment {
    private Map2D mMap;
    private Button mBtnSample;
    private Button mBtnGotoCalibration;
    private MapMarker mMarker;


    public BeaconCheckFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BeaconCheckFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BeaconCheckFragment newInstance(String param1, String param2) {
        return new BeaconCheckFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_beacon_check, container, false);
        mMap = view.findViewById(R.id.map);
        mBtnSample = view.findViewById(R.id.button_scan);

        mBtnSample.setOnClickListener(view0 -> {
            /*
            if( !CalibrationScanner.getInstance(getContext()).isStart() ) {
                startBeaconCheck();
                mMap.clearDynamic();
                mMap.invalidate();
            }
             */
        });

        mBtnGotoCalibration = view.findViewById(R.id.button_calibration);

        mBtnGotoCalibration.setOnClickListener( view0 -> {
        //    Navigation.findNavController(view0).navigate(R.id.action_check_to_calibration);
        });

        MapMarker.Builder builder = new MapMarker.Builder(this.getContext(),"marker");
        mMarker = builder.loadImage("res://com.shanlitech.bluetooth:drawable/marker")
                .setAnchor(MapMarker.ANCHOR_BOTTOM_MIDDLE)
                .setDrawSize(80,80)
                .setDraggable(true)
                .setTextMode(MapMarker.TEXT_COORDINATE)
                .setTextSize(20.f)
                .setTextColor(Color.BLUE)
                .build();
        mMap.addStaticMarker(mMarker);
        mMap.initialize(true,MapMarker.TEXT_NAME_COORDINATE);

        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        enableButtons(true);
        CalibrationScanner.getInstance(getContext()).stop();
    }

    private void enableButtons(boolean enabled) {
        mBtnGotoCalibration.setEnabled(enabled);
        mBtnSample.setEnabled(enabled);
    }

    private void startBeaconCheck() {
        //TODO
    }



    private void showAnalysis(ScanAnalysis analysis) {
        mMap.clearDynamic();
        mMarker.setCoordinate(analysis.location.x,analysis.location.y);
        for(BleBeacon b : analysis.dumb) {
            MapMarker.Builder builder = new MapMarker.Builder(this.getContext(),b.uuid);
            builder.loadImage("res://com.shanlitech.bluetooth:drawable/signal_none")
                    .setAnchor(MapMarker.ANCHOR_CENTER)
                    .setDrawSize(60,60)
                    .setDraggable(false)
                    .setCoordinate(b.location.x,b.location.y)
                    .setTextMode(MapMarker.TEXT_NAME_COORDINATE)
                    .setTextSize(20.f)
                    .setTextMode(MapMarker.TEXT_NAME)
                    .setTextColor(Color.RED);
            mMap.addDynamicMarker(builder.build());
        }
        for(BleBeacon b : analysis.choppy) {
            MapMarker.Builder builder = new MapMarker.Builder(this.getContext(),b.uuid);
            builder.loadImage("res://com.shanlitech.bluetooth:drawable/signal_red")
                    .setAnchor(MapMarker.ANCHOR_CENTER)
                    .setDrawSize(60,60)
                    .setDraggable(false)
                    .setCoordinate(b.location.x,b.location.y)
                    .setTextMode(MapMarker.TEXT_NAME_COORDINATE)
                    .setTextSize(20.f)
                    .setTextMode(MapMarker.TEXT_NAME)
                    .setTextColor(Color.RED);
            mMap.addDynamicMarker(builder.build());
        }
        for(BleBeacon b : analysis.overflow) {
            MapMarker.Builder builder = new MapMarker.Builder(this.getContext(),b.uuid);
            builder.loadImage("res://com.shanlitech.bluetooth:drawable/signal_gold")
                    .setAnchor(MapMarker.ANCHOR_CENTER)
                    .setDrawSize(60,60)
                    .setDraggable(false)
                    .setCoordinate(b.location.x,b.location.y)
                    .setTextMode(MapMarker.TEXT_NAME_COORDINATE)
                    .setTextSize(20.f)
                    .setTextMode(MapMarker.TEXT_NAME)
                    .setTextColor(Color.YELLOW);
            mMap.addDynamicMarker(builder.build());
        }
        for(BleBeacon b : analysis.underflow) {
            MapMarker.Builder builder = new MapMarker.Builder(this.getContext(),b.uuid);
            builder.loadImage("res://com.shanlitech.bluetooth:drawable/signal_blue")
                    .setAnchor(MapMarker.ANCHOR_CENTER)
                    .setDrawSize(60,60)
                    .setDraggable(false)
                    .setCoordinate(b.location.x,b.location.y)
                    .setTextMode(MapMarker.TEXT_NAME_COORDINATE)
                    .setTextSize(20.f)
                    .setTextMode(MapMarker.TEXT_NAME)
                    .setTextColor(Color.BLUE);
            mMap.addDynamicMarker(builder.build());
        }

        for(BleBeacon b : analysis.baseBeacons) {
            if( ! mMap.hasDynamicMarker(b.uuid) ) {
                MapMarker.Builder builder = new MapMarker.Builder(this.getContext(),b.uuid);
                builder.loadImage("res://com.shanlitech.bluetooth:drawable/beacon")
                        .setAnchor(MapMarker.ANCHOR_CENTER)
                        .setDrawSize(60,60)
                        .setDraggable(false)
                        .setCoordinate(b.location.x,b.location.y)
                        .setTextMode(MapMarker.TEXT_NAME_COORDINATE)
                        .setTextSize(20.f)
                        .setTextMode(MapMarker.TEXT_NAME)
                        .setTextColor(Color.BLACK);
                mMap.addDynamicMarker(builder.build());
            }
        }
        mMap.invalidate();

    }

}